﻿/****************************************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (http://rapidwebdev.org)
	Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@RapidWebDev.org

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace BaoJianSoft.RapidWeb.Controls
{
	/// <summary>
	/// Class to generate JavaScript code registered to client mapping to the control.
	/// </summary>
	public static class ControlJavaScriptGenerator
	{
		/// <summary>
		/// The separator to combine the array items into a string. "+++"
		/// </summary>
		public static readonly string ARRAY_ITEM_SEPARATOR = "+++";

		/// <summary>
		/// The httppost parameter prefix name of query field control. "QFCIndex"
		/// </summary>
		public static readonly string QUERY_FIELD_CONTROL_POST_PREFRIX_NAME = "QFCIndex";

		/// <summary>
		/// Generate JavaScript variable name registered to client for accessing control value.
		/// </summary>
		/// <param name="controlId"></param>
		/// <returns></returns>
		public static string GenerateVariableName(string controlId)
		{
			return string.Format(CultureInfo.InvariantCulture, "{0}_Variable", controlId);
		}

		/// <summary>
		/// Generate JavaScript variable name registered to client for accessing control value.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <returns></returns>
		public static string GenerateClassName<T>()
		{
			return string.Format(CultureInfo.InvariantCulture, "{0}_Class", typeof(T).Name);
		}

		/// <summary>
		/// Encode JavaScript string with single quote.
		/// </summary>
		/// <param name="s"></param>
		/// <returns></returns>
		public static string EncodeJavaScriptStringWithSingleQuote(string s)
		{
			if (string.IsNullOrEmpty(s)) return "";
			return s.Replace("'", "\\'");
		}

		/// <summary>
		/// Encode JavaScript string with single quote.
		/// </summary>
		/// <param name="s"></param>
		/// <returns></returns>
		public static string EncodeSingleQuoteToHex(string s)
		{
			if (string.IsNullOrEmpty(s)) return "";
			return s.Replace("'", "\\u0027");
		}
	}
}

